/* URBAN EXPLORER PROJECT
 * 
 * Author: Karthick Nanmaran (s1051713), School of Informatics, The University of Edinburgh
 * Email: karthicknanmaran@gmail.com
 * 
 * Supervisor: Dr.William Mackaness, 
 * 			   Institute of Geography,
 * 			   School of GeoSciences,
 * 			   The University of Edinburgh.
 * Email: william.mackaness@ed.ac.uk
 * 
 * This code can be found online at http://code.google.com/p/urban-explorers/
*/
package Firstdroid.Gps;
import com.google.android.maps.MyLocationOverlay;

import Firstdroid.Gps.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Tracker extends MainActivity 
{
	private Location loc;
	private Dialog dialog;
    /** Called when the activity is first created. */
   @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps);
        
        Button startButton = (Button) findViewById(R.id.btn_OnTrack);
        Button stopButton = (Button) findViewById(R.id.btn_OffTrack);
        
        startButton.setEnabled(false);
        stopButton.setEnabled(false);
        
        initmylocation();
    }  
        private void initmylocation(){
		/* Wait for fix or exit after 50 seconds 
	    	//Runnable to determine when the first GPS fix was received.
	        Runnable showWaitDialog = new Runnable() {
	            @Override
	            public void run() {
	            while (loc == null) {
	            // Wait for first GPS Fix (do nothing until loc != null)
	            	
	            }
	            // After receiving first GPS Fix dismiss the Progress Dialog
	            dialog.dismiss();
	            }
	            };
	            // Create a Dialog to let the User know that we're waiting for a GPS Fix
	            dialog = ProgressDialog.show(this, "Please wait...","Obtaining GPS fix... Make sure the sky is visible", true);
	            Thread t = new Thread(showWaitDialog);
	            t.start();
	                Handler handler = new Handler(); 
	                handler.postDelayed(new Runnable() { 
	                	public void run() {
	                		dialog.dismiss();
	                		}}, 50000); //Dialog box will be dismissed after 50 seconds if no fix is obtained
	                
	            while(loc !=null)
	            {*/
	            	/* START BUTTON */
	                Button startButton = (Button) findViewById(R.id.btn_OnTrack);
	            	startButton.setEnabled(true);
	                startButton.setOnClickListener(new OnClickListener() 
	                {
	                        @Override
	                        public void onClick(View arg0){
	                        		Log.d("Firstdroid.Gps", "Starting Exploration..");
	                                // Start Urban Exploration
	                                startService(new Intent(Tracker.this, UrbanExplorer.class));
	                        }
	                });
	                
	                /* STOP BUTTON */
	                Button stopButton = (Button) findViewById(R.id.btn_OffTrack);
	                stopButton.setEnabled(true);
	                stopButton.setOnClickListener(new OnClickListener() 
	                {
	                        @Override
	                        public void onClick(View v) 
	                        {
	                        Log.d("Firstdroid.Gps", "Stopping Exploration..");
	                        //Stop Urban Exploration
	                        stopService(new Intent(Tracker.this, UrbanExplorer.class));
	                        //System.exit(0);
	                }
	                });
	            
 
    }

}/* End of MainActivity */